import { register } from 'register-service-worker';
import { dispatchServiceWorkerEvent } from './sw-event'

export default function(swDest) {
  register(`${swDest}`, {
    registered(registration) {
      console.log('service worker(registered) 已注册成功');
      dispatchServiceWorkerEvent('sw.registered', registration);
    },
    updatefound(registration) {
      console.log('service worker(updatefound) 发现更新');
      dispatchServiceWorkerEvent('sw.updatefound', registration);
    },
    cached(registration) {
      console.log('service worker(cached) 已将更新缓存');
      dispatchServiceWorkerEvent('sw.cached', registration);
    },
    ready(registration) {
      console.log('service worker(ready) 已可以使用缓存提供服务');
      dispatchServiceWorkerEvent('sw.ready', registration);
    },
    updated(registration) {
      console.log('service worker(updated) 新配置更新成功,请刷新');
      dispatchServiceWorkerEvent('sw.updated', registration);
    },
    offline() {
      console.log('service worker(offline) 网络连接失败.应用程序正在脱机模式下运行.');
      dispatchServiceWorkerEvent('sw.offline', {});
    },
    error(error) {
      console.log('service worker 注册报错:',error);
      dispatchServiceWorkerEvent('sw.error', error);
    },
  });
}
